#!/usr/bin/make -f

# ####################################################################
# Description:  rules
#
#               This file, 'rules', implements build targets for Debian
#				style packaging
#
# Copyright (C) 2021    Jakub Fišer  <jakub DOT fiser AT eryaf DOT com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# #####################################################################

CURRENT_DEBIAN_VERSION :=$(shell grep "^VERSION_ID=" /etc/os-release | cut -d'=' -f2 | tr -d '"')
DEBIAN_BULLSEYE_VERSION :=11

#	Turn OFF the Link-Time Optimization (LTO) during compilation as it clashes with the
#	current version of linker script turning symbols of HAL modules local (defined
#	in 'MachinekitHALSymbolVisibilityFunction.cmake' CMake file)
DEB_CFLAGS_MAINT_STRIP :=-flto=auto -ffat-lto-objects
DEB_CXXFLAGS_MAINT_STRIP :=-flto=auto -ffat-lto-objects
DEB_LDFLAGS_MAINT_STRIP :=-flto=auto

#	Set the main optimization value for all parts to O0
DEB_CFLAGS_MAINT_STRIP +=-O2
DEB_CXXFLAGS_MAINT_STRIP +=-O2
DEB_CFLAGS_MAINT_APPEND :=-O0
DEB_CXXFLAGS_MAINT_APPEND :=-O0

#	Do not warn on potential reproducible build datetime mismatch
DEB_CPPFLAGS_MAINT_STRIP :=-Wdate-time

#	Python3 distro specific paths used for installation of Pythonic
#	packages to the system
#	TODO: Are these paths right?
MACHINEKIT_HAL_PYTHON_STDLIB_FULL_INSTALL_DIRECTORY :=/usr/lib
MACHINEKIT_HAL_PYTHON_PURELIB_FULL_INSTALL_DIRECTORY :=/usr/lib/python3/dist-packages
MACHINEKIT_HAL_PYTHON_PLATSTDLIB_FULL_INSTALL_DIRECTORY :=/usr/lib/python3
MACHINEKIT_HAL_PYTHON_PLATLIB_FULL_INSTALL_DIRECTORY :=/usr/lib/python3/dist-packages
MACHINEKIT_HAL_PYTHON_INCLUDE_FULL_INSTALL_DIRECTORY :=/usr/include
MACHINEKIT_HAL_PYTHON_PLATINCLUDE_FULL_INSTALL_DIRECTORY :=/usr/include
MACHINEKIT_HAL_PYTHON_SCRIPTS_FULL_INSTALL_DIRECTORY :=/usr/bin
MACHINEKIT_HAL_PYTHON_DATA_FULL_INSTALL_DIRECTORY :=/usr
MACHINEKIT_HAL_PYTHON_INTERPRETER_INSTALL_EXECUTABLE :=/usr/bin/python3

export DEB_CFLAGS_MAINT_STRIP
export DEB_CXXFLAGS_MAINT_STRIP
export DEB_LDFLAGS_MAINT_STRIP
export DEB_CFLAGS_MAINT_APPEND
export DEB_CXXFLAGS_MAINT_APPEND
export DEB_CPPFLAGS_MAINT_STRIP

override_dh_auto_configure:
	dh_auto_configure -- \
		--toolchain $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))/debianMultiarchToolchain.cmake \
		-DMACHINEKIT_HAL_PYTHON_STDLIB_FULL_INSTALL_DIRECTORY=$(MACHINEKIT_HAL_PYTHON_STDLIB_FULL_INSTALL_DIRECTORY) \
		-DMACHINEKIT_HAL_PYTHON_PURELIB_FULL_INSTALL_DIRECTORY=$(MACHINEKIT_HAL_PYTHON_PURELIB_FULL_INSTALL_DIRECTORY) \
		-DMACHINEKIT_HAL_PYTHON_PLATSTDLIB_FULL_INSTALL_DIRECTORY=$(MACHINEKIT_HAL_PYTHON_PLATSTDLIB_FULL_INSTALL_DIRECTORY) \
		-DMACHINEKIT_HAL_PYTHON_PLATLIB_FULL_INSTALL_DIRECTORY=$(MACHINEKIT_HAL_PYTHON_PLATLIB_FULL_INSTALL_DIRECTORY) \
		-DMACHINEKIT_HAL_PYTHON_INCLUDE_FULL_INSTALL_DIRECTORY=$(MACHINEKIT_HAL_PYTHON_INCLUDE_FULL_INSTALL_DIRECTORY) \
		-DMACHINEKIT_HAL_PYTHON_PLATINCLUDE_FULL_INSTALL_DIRECTORY=$(MACHINEKIT_HAL_PYTHON_PLATINCLUDE_FULL_INSTALL_DIRECTORY) \
		-DMACHINEKIT_HAL_PYTHON_SCRIPTS_FULL_INSTALL_DIRECTORY=$(MACHINEKIT_HAL_PYTHON_SCRIPTS_FULL_INSTALL_DIRECTORY) \
		-DMACHINEKIT_HAL_PYTHON_DATA_FULL_INSTALL_DIRECTORY=$(MACHINEKIT_HAL_PYTHON_DATA_FULL_INSTALL_DIRECTORY) \
		-DMACHINEKIT_HAL_PYTHON_INTERPRETER_INSTALL_EXECUTABLE=$(MACHINEKIT_HAL_PYTHON_INTERPRETER_INSTALL_EXECUTABLE)

override_dh_auto_test:
#	Only run tests (CTest) when not cross-compiling
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	dh_auto_test --
else
$(warning "CROSS-COMPILATION: Not running tests.")
endif

override_dh_fixperms:
	dh_fixperms
	find debian -type f \( -name 'rtapi_app' -o -name 'pci_write' -o -name 'pci_read' \) -exec chmod 4755 {} \;

#	Change to more modern version after support for Ubuntu 18.04 Bionic is dropped
#execute_after_dh_fixperms:
#	find debian -type f \( -name 'rtapi_app' -o -name 'pci_write' -o -name 'pci_read' \) -exec chmod 4755 {} \;

override_dh_dwz:
#	Debian 11 Bullseye started to have a problem with Machinekit-HAL's machinekit-hal-testsuite-runtests
#	Situation should be solved in dwz version 0.14-1 (version in Ubuntu 21.04 Hirsute)
#	The ERROR: '.debug_line reference above end of section' error'
ifeq ($(CURRENT_DEBIAN_VERSION), $(DEBIAN_BULLSEYE_VERSION))
$(warning "Debian Bullseye: Not going to run the dwz tool!")
else
	dh_dwz
endif

# This is hopefully a temporary hack (but HACK nonetheless) to install the CMake configuration files
# for `find_package()` calls as there is a problem with CPack based installation as outlined
# in https://discourse.cmake.org/t/installing-cmake-exports-with-cpack/4718 and waiting for an expert help
override_dh_install:
	cd debian/tmp && find . -iwholename '*/Machinekit-HAL/*.cmake' -exec mkdir \
		-p ../libmachinekit-hal-dev/$(dirname {}) \; -exec cp {} ../libmachinekit-hal-dev/{} \;

%:
	dh $@ --buildsystem=cmake --with=python3
